﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace mesoBoard.Data.Repositories
{
    public class OnlineUserRepository : IOnlineUserRepository
    {
        private mbEntities db;

        public OnlineUserRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public OnlineGuest GetOnlineGuest(string IP)
        {
            return db.OnlineGuests.FirstOrDefault(x => x.IP == IP);
        }

        public bool UserIsGuest(string IP)
        {
            return GetOnlineGuest(IP) != null;
        }

        public IEnumerable<OnlineGuest> GetOnlineGuests()
        {
            return db.OnlineGuests.AsEnumerable();
        }

        public void CreateOnlineGuest(OnlineGuest TheOGuest)
        {
            db.OnlineGuests.AddObject(TheOGuest);
            db.SaveChanges();
        }

        public void DeleteOnlineGuest(string IP)
        {
            db.OnlineGuests.DeleteObject(GetOnlineGuest(IP));
            db.SaveChanges();
        }

        public void UpdateOnlineUser(OnlineUser TheOUser)
        {
            if (TheOUser.EntityKey == null)
                TheOUser.EntityKey = ((OnlineUser)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.OnlineUsers", "UserID", TheOUser.UserID))).EntityKey;

            db.OnlineUsers.ApplyCurrentValues(TheOUser);
            db.SaveChanges();
        }

        public void UpdateOnlineGuest(OnlineGuest onlineGuest)
        {
            if (onlineGuest.EntityKey == null)
                onlineGuest.EntityKey = ((OnlineGuest)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.OnlineGuests", "OnlineGuestID", onlineGuest.OnlineGuestID))).EntityKey;

            db.OnlineGuests.ApplyCurrentValues(onlineGuest);
            db.SaveChanges();
        }

        public IEnumerable<OnlineUser> GetOnlineUsers()
        {
            return db.OnlineUsers.AsEnumerable();
        }

        public void CreateOnlineUser(OnlineUser TheOUser)
        {
            db.OnlineUsers.AddObject(TheOUser);
            db.SaveChanges();
        }

        public OnlineUser GetOnlineUser(int UserID)
        {
            return db.OnlineUsers.FirstOrDefault(x => x.UserID == UserID);
        }

        public void DeleteOnlineUser(int UserID)
        {
            OnlineUser ou = db.OnlineUsers.FirstOrDefault(x => x.UserID == UserID);
            if (ou != null)
            {
                db.OnlineUsers.DeleteObject(ou);
                db.SaveChanges();
            }
        }

        public void DeleteAllOnlineUsers()
        {
            foreach (OnlineUser ou in db.OnlineUsers)
            {
                db.OnlineUsers.DeleteObject(ou);
            }
            db.SaveChanges();
        }

        public void DeleteOnlineUsers(IEnumerable<OnlineUser> users)
        {
            foreach (OnlineUser o in users)
            {
                db.DeleteObject(o);
            }
            db.SaveChanges();
        }

        public void DeleteOnlineGuests(IEnumerable<OnlineGuest> guests)
        {
            foreach (OnlineGuest o in guests)
            {
                db.DeleteObject(o);
            }
            db.SaveChanges();
        }

        public void PruneOnlineUsers()
        {
            var users = db.Get_Inactive_OnlineUsers();
            
            foreach (OnlineUser ou in users)
            {
                db.OnlineUsers.DeleteObject(ou);
            }
            db.SaveChanges();            
        }
        
        public void PruneOnlineGuests()
        {
            var users = db.Get_Inactive_OnlineGuests();

            foreach (OnlineGuest ou in users)
            {
                db.OnlineGuests.DeleteObject(ou);
            }

            db.SaveChanges();            
        }

        public void SubmitChanges()
        {
            db.SaveChanges();
        }
    }
}
